package net.wicp.tams.common.redis.pool;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import net.wicp.tams.common.Conf;
import net.wicp.tams.common.apiext.StringUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class JedisPoolTams extends AbsPool {
	private volatile JedisPool jedisPool;

	public JedisPoolTams() {
	}

	public JedisPoolTams(final GenericObjectPoolConfig<Jedis> poolConfig, final String host, final int port,
			final int timeout) {
		poolConfig.setJmxEnabled(true);
		poolConfig.setJmxNamePrefix("jedis-pool");
		JedisPool jedisPool = new JedisPool(poolConfig, host, port, timeout,
				StringUtil.hasNull(Conf.get("common.redis.redisserver.password"), null),
				Conf.getInt("common.redis.redisserver.defaultDb"), Conf.get("common.redis.redisserver.clientName"));
		setJedisPool(jedisPool);
	}

	public void setJedisPool(JedisPool jedisPool) {
		doLeak(jedisPool);
		this.jedisPool = jedisPool;
	}

	

	@Override
	public Jedis getResource() {
		return this.jedisPool.getResource();
	}

	
	@Override
	public void destroy() {
		this.jedisPool.destroy();
		this.jedisPool = null;
	}
	@Override
	public boolean isInit() {
		return this.jedisPool != null;
	}

}
